package com.younio.local.entity;

import lombok.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Jb {

    private int id;

    private String tb;

    private int index;

    private String result;

    private List<Qn> qnList;

    private String qns;

    private String tid;


    private String getTbNo(String tbNo) {
        if (tb == null) {
            return "";
        }
        return tbNo;
    }

    public void init() {
        if (qnList != null) {
            this.qns = getQns();
        }

        this.tb = getTbNo(tb);

        this.result = "1".equals(result) ? "合格" : "不合格";
    }

    /**
     * @desc Qn -> Q1:0.019948317136276917, Q2:0.0039999999999937655, Q3:-0.0010000000000001674
     * 先拍序  小 -> 大
     * Q * 10000绝对值取整, n * 1000
     * concat(n * 1000 + Q * 10000) + count -> toHex
     * @author lan [1728209643@qq.com]
     * @create 2019/1/3 10:29
     */
    private String getQns() {
        qnList.sort(Comparator.comparing(Qn::getVal));
        long count = qnList.stream().filter(qn -> qn.getVal().compareTo(BigDecimal.ZERO) < 0).count();
        qnList.forEach(qn -> qn.init(4, 4));
        String reduce = qnList.stream().reduce("", (s, qn) -> s + qn.getValue(), (s, s2) -> s + s2);
        return new BigInteger(reduce + count).toString(16);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Jb)) return false;
        Jb jb = (Jb) o;
        return getIndex() == jb.getIndex();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getIndex());
    }
}
